/**
 *  login
 */
let desKey="";
let _loading=false;
layui.use(['form','jquery'], function () {
    var $ = layui.jquery,
        form = layui.form,
        layer = layui.layer;
    // 登录过期的时候，跳出ifram框架
    // if (top.location != self.location) top.location = self.location;
    let userName=localStorage.getItem("userName");
    if(userName!=undefined && userName!='')
    {
        $("#userName").val(decryptLocalStorage(userName));
    }
    let u=localStorage.getItem("a@m");
    if(u!=undefined && u!=""){
        let loginU=decryptLocalStorage(u);
        request.token({token:loginU.token},function(data) {if(data.data==1){window.location = '../index';}});
    }
    $('.bind-password').on('click', function () {
        if ($(this).hasClass('icon-5')) {
            $(this).removeClass('icon-5');
            $("input[name='password']").attr('type', 'password');
        } else {
            $(this).addClass('icon-5');
            $("input[name='password']").attr('type', 'text');
        }
    });
    $('.icon-nocheck').on('click', function () {
        if ($(this).hasClass('icon-check')) {
            $(this).removeClass('icon-check');
        } else {
            $(this).addClass('icon-check');
        }
    });

    // 进行登录操作
    $(".login-btn").on('click', function () {
        loginSubmit();
    });
});
$(function(){
    loginInit();
    $(this).keydown(function(event) {
        if (event.keyCode == 13) {
            loginSubmit();
        }
    });

});
function loginInit() {request.loginInit({},function(data){desKey=data.data;});}
function loginSubmit()
{
    let userName=$("input[name='userName']").val();
    let password=$("input[name='password']").val();
    let verifyCode=$("input[name='verifyCode']").val();
    if (userName == '') {
        layer.msg('账号不能为空');
        $("input[name='userName']").focus();
        return false;
    }
    if (password == '') {
        layer.msg('密码不能为空');
        $("input[name='password']").focus();
        return false;
    }
    if (verifyCode == '') {
        layer.msg('验证码不能为空');
        $("input[name='verifyCode']").focus();
        return false;
    }
    let user = encryptByDES(userName, desKey);
    password = encryptByDES(password, desKey);
    verifyCode = encryptByDES(verifyCode, desKey);
    if(desKey=='')
    {
        layer.msg('系统异常，请联系管理！');
        $("input[name='verifyCode']").focus();
        return false;
    }
    let params={userName:user,password:password,verifyCode:verifyCode};
    if(_loading)
    {
        return false;
    }
    layer.load(2,{shade: [0.5, '#fff']});
    _loading=true;
    request.login(params,function(data){
        if(data.code==0){
            localStorage.setItem("userName",encryptLocalStorage(userName));
            layer.msg('登录成功', {
                icon: 1,
                time: 10
            },function () {
                window.location = '../index';
                layer.closeAll();
            });
        }else{
            _loading=false;
            layer.closeAll();
            layer.msg(data.msg || '验证码失效，请重新登录', {
                icon: 2,
                time: 2000
            });
            loginInit();
            let src=$("#refreshCaptcha").attr("src");
            $("#refreshCaptcha").attr("src",src+"?");
            if(data.msg!='验证码错误')
            {
                $("input[name='password']").focus();
            }else{
                $("input[name='verifyCode']").focus();
            }

        }
    });
}